package com.codi.fcloud.ufx.hssale.portfolio;

import com.codi.base.log.annotation.Loggable;
import com.codi.base.util.ExceptionUtil;
import com.codi.bus.exception.HundsunAPIException;
import com.codi.fcloud.ufx.UfxSDKFactory;
import com.codi.fcloud.ufx.constant.HSInputParameterNames;
import com.codi.fcloud.ufx.constant.UrlConstants;
import com.codi.trade.dto.SharePortfolioCodeQryDto;
import com.codi.trade.service.portfolio.HsSharePortfolioCodeQryService;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by asus on 2017/11/6.
 */
@Slf4j
@Service
public class HsSharePortfolioCodeQryServiceImpl extends UfxSDKFactory implements HsSharePortfolioCodeQryService {
    @Override
    @Loggable
    public Map<String, Object> query(SharePortfolioCodeQryDto dto) {
        Preconditions.checkNotNull(dto);
        Preconditions.checkState(!(Strings.isNullOrEmpty(dto.getTrade_acco())
                && Strings.isNullOrEmpty(dto.getClient_id())
                && Strings.isNullOrEmpty(dto.getTa_acco())),
            "交易账号、TA账号、客户编号不能同时为空");
        String              trustWay = apiValueMap.get(HSInputParameterNames.TRUST_WAY);
        Map<String, Object> request  = new HashMap<>();

        request.put("trust_way", trustWay);
        request.put("net_no", getString(dto.getNet_no(), "0001"));


        request.put("request_num", getString(dto.getRequest_num(), "50")); //	N8	请求行数	Y	最大不能超过50；即最大返回记录条数不会超过50条；
        request.put("reqry_recordsum_flag", getString(dto.getReqry_recordsum_flag(), "1"));//	C1	重新统计总记录数标志	Y	0-不重新统计；1-重新统计；
        request.put("sort_direction", getString(dto.getSort_direction()));//	C1	返回排序方式	N
        request.put("trade_acco", getString(dto.getTrade_acco())); //	C17	交易账号	N
        request.put("ta_acco", getString(dto.getTa_acco()));//	C12	TA账号	N
        request.put("client_id", getString(dto.getClient_id()));//	C18	客户编号	N	交易账号与客户编号至少有一个为必填项

        //	N8	查询起始行号	N	比如总记录是100，每50条分页；那么需要查询2次；第1次查询请求行数传50，重新统计总记录数传1，查询起始行号private String 传1；第2次查询请求行数传50，重新统计总记录数传0，查询起始行号传51；
        request.put("qry_beginrownum", getString(dto.getQry_beginrownum()));

        request.put("password", getString(dto.getPassword()));//	C50	密码 N

        request.put("fund_code", getString(dto.getFund_code()));//	C6	基金代码	N
        request.put("ofund_type", getString(dto.getOfund_type()));//	C1	基金类别	N
        request.put("portfolio_code", getString(dto.getPortfolio_code()));//	C4	组合编号	N
        try {
            Map<String, Object> response = getUfxSDK().transact(getTarget(), request);

            return appendLog(request, response);
        } catch (Exception exception) {
            ExceptionUtil.logError(logger, getTarget(), exception);
            throw new HundsunAPIException(ExceptionUtil.getMessage(exception));
        }
    }

    @Override
    public SharePortfolioCodeQryDto getRequestDto(String requestNum, String queryflag, String sortDirection, String tradeAccount, String taAccount, String clientId, String password, String beginNum, String fundCode, String shareType, String fundType, String portfolioCode){
        SharePortfolioCodeQryDto sharePortfolioCodeQryDto = new SharePortfolioCodeQryDto();
        sharePortfolioCodeQryDto.setRequest_num(requestNum);
        sharePortfolioCodeQryDto.setReqry_recordsum_flag(queryflag);
        sharePortfolioCodeQryDto.setSort_direction(sortDirection);
        sharePortfolioCodeQryDto.setTrade_acco(tradeAccount);
        sharePortfolioCodeQryDto.setTa_acco(taAccount);
        sharePortfolioCodeQryDto.setClient_id(clientId);
        sharePortfolioCodeQryDto.setPassword(password);
        sharePortfolioCodeQryDto.setQry_beginrownum(beginNum);
        sharePortfolioCodeQryDto.setFund_code(fundCode);
        sharePortfolioCodeQryDto.setShare_type(shareType);
        sharePortfolioCodeQryDto.setOfund_type(fundType);
        sharePortfolioCodeQryDto.setPortfolio_code(portfolioCode);
        return sharePortfolioCodeQryDto;
    }

    @Override
    protected String getTarget() {
        return UrlConstants.PREFIX_SALE + UrlConstants.SHARE_PORTFOLIO_CODE_QRY;
    }
}
